'''
Created on Mar 24, 2013

@author: Petko
'''
from django.views.generic.base import View
from sent.utils import CommonResult, ErrorIds, createValidToken
from sent.utils.result_printer import getPrinter
from sent.model.UserDBManager import UserDBManager

class RemoveUserFromGroup(View):
    '''
    classdocs
    '''


    def __init__(self):
        '''
        Constructor
        '''
    
    def post(self, *args, **kwargs):
        accessTokenStr = self.request.POST.get('access_token', None)
        group_id = self.request.POST.get('group_id', None)
        user_id = self.request.POST.get('user_id', None)
        
        result = CommonResult()
        
        if accessTokenStr and group_id and user_id:
            accessToken, error = createValidToken(accessTokenStr, 'group_adm')
            if accessToken:
                dbMan = UserDBManager(False)
                try:
                    deletedCnt = dbMan.removeUserFromGroup(user_id, group_id)
                    if deletedCnt:
                        result.errorId = ErrorIds.SUCCESS
                        result.result = { 'success': True }
                        dbMan.commitSession()
                    else:
                        result.errorId = ErrorIds.NOT_FOUND
                        result.result = "User or group not found %s %s" % (user_id, group_id)
                        dbMan.rollbackSession()
                except:
                    dbMan.rollbackSession()
            else:
                result.errorId = ErrorIds.UNAUTHORIZED
                result.result = error
        else:
            result.errorId = ErrorIds.BADREQUEST
            result.result = "Missing parameter"
            
        return getPrinter().printResult(result) 